package cz.khfotbal.beans;

import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.khfotbal.domain.Article;
import cz.khfotbal.service.ArticleService;

@Component
@Scope(value = "session")
public class ArticleBean {

	@Autowired
	private ArticleService articleService;

	private Article newArticle;
	private Article selectedArticle;

	private String category;
	private String categoryName;

	private int idArticle;
	
	
	
	
	
	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public ArticleBean() {
		newArticle = new Article();
	}

	public void deleteArticle() {
		if (selectedArticle != null) {
			articleService.deleteArticle(selectedArticle);
		}
	}
	
	public List<Article> getArticleListMain(){
		return articleService.getAllArticlesMain();
	}

	public List<Article> getArticleListVybor() {
		return articleService.getAllArticles(category);
	}

	public List<Article> getArticleListSTKDCK() {
		return articleService.getAllArticles("stkdck");
	}
	
	public List<Article> getArticleListDK() {
		return articleService.getAllArticles("dk");
	}
	
	public List<Article> getArticleListVykVybor() {
		return articleService.getAllArticles("vybor");
	}
	
	public List<Article> getArticleListKomiseRozhodcich() {
		return articleService.getAllArticles("komiserozhodcich");
	}
	
	public List<Article> getArticleListKomiseMladeze() {
		return articleService.getAllArticles("komisemladeze");
	}
	
	public List<Article> getArticleListZpravySekretariatu() {
		return articleService.getAllArticles("zpravysekretariatu");
	}
	
	public List<Article> getArticleListTmk() {
		return articleService.getAllArticles("tmk");
	}
	
	public List<Article> getArticleListOdvolavaciKomise() {
		return articleService.getAllArticles("odvolavacikomise");
	}
	
	public List<Article> getArticleInformace() {
		return articleService.getAllArticles("informace");
	}
	
	public List<Article> getArticleClanky() {
		return articleService.getAllArticles("clanky");
	}
	
	public Article getArticleSms() {
		List<Article> articles = articleService.getAllArticles("smsrozhodci");
		Article result = new Article();
		for (Article article : articles){
			result = article;
		}
		return result;
	}
	
	public List<Article> getArticleListInformaceMain() {
		return articleService.getFiveInformation();	
	}
	
	public List<Article> getArticleListClankyMain() {
		return articleService.getFiveClanek();
	}

	public void updateArticle() {
		if (selectedArticle != null) {
			articleService.updateArticle(selectedArticle);
		}
	}

	public void vyborArticle() {
		category = "vybor";
	}

	public void createArticleVybor() {
		newArticle.setCategory(category);
		newArticle.setDate(new Date());
		articleService.createArticle(newArticle);
		newArticle = new Article();
	}

	public void setNewArticle(Article newArticle) {
		this.newArticle = newArticle;
	}

	public Article getArticleById() {
		String parameter = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get( "id" );
		int id = Integer.parseInt(parameter);
		return articleService.getArticleById(id);
	}

	public Article getNewArticle() {
		return newArticle;
	}

	public Article getSelectedArticle() {
		if (selectedArticle == null) {
			selectedArticle = new Article();
			selectedArticle.setContent("popis");
			selectedArticle.setName("nazev");
		}
		return selectedArticle;
	}

	public void setSelectedArticle(Article selectedArticle) {
		this.selectedArticle = selectedArticle;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public int getIdArticle() {
		return idArticle;
	}

	public void setIdArticle(int idArticle) {
		this.idArticle = idArticle;
	}
}
